using System;
using System.Security.Cryptography;
using System.Text;

namespace Read.Utility
{
    public class Tools
    {
        /// <summary>
		/// 获取当前时间戳
		/// </summary>
		public static int ToUnixTime()
        {
            var time = DateTime.Now;
            DateTimeOffset dto = new DateTimeOffset(time);
            return (int)dto.ToUnixTimeSeconds();
        }

        /// <summary>
        /// DateTime转换成时间戳
        /// </summary>
        /// <param name="time">DateTime</param>
        /// <returns></returns>
        public static int ToUnixTime(DateTime time)
        {
            DateTimeOffset dto = new DateTimeOffset(time);
            return (int)dto.ToUnixTimeSeconds();
        }

        /// <summary>
        /// 时间戳转换成(东八区)Datetime
        /// </summary>
        /// <param name="timeStamp">时间戳</param>
        /// <returns></returns>
        public static DateTime FromUnixTime(int timeStamp)
        {
            long lTime = long.Parse(timeStamp.ToString());
            DateTimeOffset dtoffset = DateTimeOffset.FromUnixTimeSeconds(lTime);
            return dtoffset.DateTime.AddHours(8);
        }

        /// <summary>
        /// 时间戳转换成东八区时间(格式化)
        /// </summary>
        /// <param name="timeStamp">时间戳</param>
        /// <param name="format">格式化字符串如("yyyy-MM-dd")</param>
        /// <returns></returns>
        public static string FromUnixTime(int timeStamp, string format = "yyyy-MM-dd")
        {
            if (!string.IsNullOrEmpty(timeStamp.ToString()) && timeStamp.ToString().Trim().Length == 10)
            {
                long lTime = long.Parse(timeStamp.ToString());
                DateTimeOffset dtoffset = DateTimeOffset.FromUnixTimeSeconds(lTime);
                return dtoffset.DateTime.AddHours(8).ToString(format);
            }
            else
            {
                return string.Empty;
            }
        }
        
        /// <summary>
        /// 随机字符串
        /// </summary>
        public static string RandomString(int size)
        {
            var arr = new string[]{
                "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M",
                "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
                "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m",
                "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
                "1","2","3","4","5","6","7","8","9"};
            var sb = new StringBuilder();

            var iTick = DateTime.Now.Ticks;
            for (int i = 1; i <= size; i++)
            {
                var r = new Random((int)iTick * i).Next(0, arr.Length - 1);
                sb.Append(arr[r]);
                iTick += (new Random((int)iTick).Next(999));
            }

            return sb.ToString();
        }

        /// <summary>
        /// 密码加密
        /// </summary>
        public static string HasPassword(string password, string verify)
        {
            return $"{password}{verify.ToMD5()}".ToMD5();
        }
    }
}